﻿//Testov class s Main metod.
using System;
using System.Net;
using System.Threading;
using System.IO;

class TestMain
{
    public static void Reader()
    {
        int milsecs = 60;
        introShown = true;
           

        using (StreamReader input = new StreamReader("../../storyline.txt"))
        {
            for (string line; (line = input.ReadLine()) != null; )
            {
                string[] words = line.Split(' ');
                for (int i = 0; i < words.Length; i++)
                {

                    Console.Write(words[i] + " ");
                    Thread.Sleep(milsecs);
                }
                                
                Console.WriteLine();
            }
            Console.ReadLine();
            Console.Clear();
            

        }
    }

    public static bool introShown = false;
    

    static void Main()
    {
        Console.Title = "WARRIORS";

        if (!introShown)
        {
          Reader();
        }
        
        int playerGold=0;
        int counter = 1;
        Board board = new Board();
        string[,] boardTest = new string[11, 17];
        boardTest = board.InitialBoardDraw(boardTest);

        Console.Write("First of all, lets start with entering the amount of money each king has: ");
        try
        {
            playerGold = int.Parse(Console.ReadLine());
        }
        catch(FormatException ex)
        {
            Console.Clear();
            Console.WriteLine(ex.Message);
            Main();
        }

        Player p1 = new Player("P1");

        Console.Clear();

        p1.Market(playerGold);
        
        Unit[] army1 = new Unit[5];

        army1[0] = new Warrior(p1.PName, p1.WAmount);
        army1[1] = new Knight(p1.PName, p1.KAmount);
        army1[2] = new Archer(p1.PName, p1.AAmount);
        army1[3] = new Barbarian(p1.PName, p1.BAmount);
        army1[4] = new Pikeman(p1.PName, p1.PAmount);

        Console.Clear();

       
        Player p2 = new Player("P2");
        p2.Market(playerGold);
        Unit[] army2 = new Unit[5];
        army2[0] = new Warrior(p2.PName, p2.WAmount);
        army2[1] = new Knight(p2.PName, p2.KAmount);
        army2[2] = new Archer(p2.PName, p2.AAmount);
        army2[3] = new Barbarian(p2.PName, p2.BAmount);
        army2[4] = new Pikeman(p2.PName, p2.PAmount);

        
        while (true)
        {
            if(counter%2==1)
            {
                boardTest = board.BoardPiecesStartDraw(army1, boardTest, p1);
                boardTest = board.BoardPiecesStartDraw(army2, boardTest, p2);
                board.BoardFinalDraw(boardTest);
                Action.UnitAction(p1, army1, army2, boardTest , 2);
                Console.Clear();
            }

            else
            {
                boardTest = board.BoardPiecesStartDraw(army1, boardTest, p1);
                boardTest = board.BoardPiecesStartDraw(army2, boardTest, p2);
                board.BoardFinalDraw(boardTest);
                Action.UnitAction(p2, army2, army1, boardTest, 2);
                Console.Clear();
            }
            if (army1[0].isDead() && army1[1].isDead() && army1[2].isDead() && army1[3].isDead() && army1[4].isDead() && army2[0].isDead() && army2[1].isDead() && army2[2].isDead() && army2[3].isDead() && army2[4].isDead())
            {
                Console.Clear();
                Console.WriteLine("Game ends in a TIE!!!");
                Console.ReadLine();
                break;
            }
            else if (army1[0].isDead() && army1[1].isDead() && army1[2].isDead() && army1[3].isDead() && army1[4].isDead())
            {
                Console.Clear();
                Console.WriteLine("Player 2 WINS!!!\nCongratulations Hero, you deserve the title The Savior of The Realm!!!");
                Console.ReadLine();
                break;
            }
            else if (army2[0].isDead() && army2[1].isDead() && army2[2].isDead() && army2[3].isDead() && army2[4].isDead())
            {
                Console.Clear();
                Console.WriteLine("Player 1 WINS!!!\nCongratulations Hero, you deserve the title The Savior of The Realm!!!");
                Console.ReadLine();
                break;
            }

            counter++;
        }

    }
}

